#!/bin/sh
#
# /etc/init.d/cloudstack-management -- startup script for CloudStack

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
### BEGIN INIT INFO
# Provides:          cloudstack-management
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start CloudStack.
# Description:       Start the CloudStack Management Server.
### END INIT INFO

PATH=/bin:/usr/bin:/sbin:/usr/sbin
NAME=cloudstack-management
DESC="CloudStack Management Server"
DAEMON=/usr/bin/jsvc
DEFAULT=/etc/default/$NAME
USER="cloud"
GROUP="cloud"
CLOUDSTACK_PID="/var/run/$NAME.pid"

# We have to explicitly set the HOME variable to the homedir from the user "cloud"
# This is because various scripts run by the management server read the HOME variable
# and fail when this init script is run manually.
HOME=$(echo ~cloud)

if [ `id -u` -ne 0 ]; then
	echo "You need root privileges to run this script"
	exit 1
fi

if [ -r /etc/default/locale ]; then
	. /etc/default/locale
	export LANG
fi

. /lib/lsb/init-functions
. /etc/default/rcS

# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not
# defined in $DEFAULT)
JDK_DIRS="/usr/lib/jvm/java-8-openjdk-amd64 /usr/lib/jvm/java-8-openjdk-i386 /usr/lib/jvm/java-8-oracle /usr/lib/jvm/java-8-openjdk"

# Look for the right JVM to use
for jdir in $JDK_DIRS; do
    if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then
    JAVA_HOME="$jdir"
    fi
done
export JAVA_HOME

# overwrite settings from default file
if [ -f "$DEFAULT" ]; then
	. "$DEFAULT"
fi

JARS=$(ls /usr/share/cloudstack-management/lib/*.jar | tr '\n' ':' | sed s'/.$//')
CLASSPATH="$JARS:$CLASSPATH"

[ -f "$DAEMON" ] || exit 0

# Look for Java Secure Sockets Extension (JSSE) JARs
if [ -z "${JSSE_HOME}" -a -r "${JAVA_HOME}/jre/lib/jsse.jar" ]; then
    JSSE_HOME="${JAVA_HOME}/jre/"
fi
export JSSE_HOME

case "$1" in
  start)
	if [ -z "$JAVA_HOME" ]; then
		log_failure_msg "no JDK found - please set JAVA_HOME"
		exit 1
	fi

	log_daemon_msg "Starting $DESC" "$NAME"
	if start-stop-daemon --test --start --quiet --pidfile "$CLOUDSTACK_PID" \
		--user $USER --startas "$JAVA_HOME/bin/java"; then

		# fix storage issues on nfs mounts
		umask 000
		start-stop-daemon --start --quiet --pidfile "$CLOUDSTACK_PID" \
		--user $USER --group $GROUP --exec $DAEMON -- -user "$USER" -server \
		-home "$JAVA_HOME" -cp "$CLASSPATH" \
		-outfile SYSLOG -errfile $LOGDIR/$NAME.err \
		-pidfile "$CLOUDSTACK_PID" $JAVA_OPTS "$BOOTSTRAP_CLASS"
		log_end_msg $?
	else
		log_progress_msg "(already running)"
		log_end_msg 0
	fi
	;;
  stop)
	log_daemon_msg "Stopping $DESC" "$NAME"
	start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile "$CLOUDSTACK_PID"
	RETVAL="$?"
	[ "$RETVAL" = 2 ] && return 2
	log_end_msg 0
	;;
   status)
        if start-stop-daemon --test --start --pidfile "$CLOUDSTACK_PID" \
		--user $USER --startas "$JAVA_HOME/bin/java" \
		>/dev/null; then

		if [ -f "$CLOUDSTACK_PID" ]; then
		    log_success_msg "$DESC is not running, but pid file exists."
			exit 1
		else
		    log_success_msg "$DESC is not running."
			exit 3
		fi
	else
		log_success_msg "$DESC is running with pid `cat $CLOUDSTACK_PID`"
	fi
        ;;
  restart|force-reload)
        if start-stop-daemon --test --stop --pidfile "$CLOUDSTACK_PID" \
		--user $USER --startas "$JAVA_HOME/bin/java" \
		>/dev/null; then
		$0 stop
		sleep 1
	fi
	$0 start
	;;
  *)
	log_success_msg "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
	exit 1
	;;
esac

exit 0
